package com.yhm.pagination;

import com.opensymphony.xwork2.util.ValueStack;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

/**
 * 分页标签 *
 * @author 杨华明
 * @date 2012.9.28
 *
 */
public class PageTag extends ComponentTagSupport {
	private static final long serialVersionUID = 7242423813230124088L;
	private Logger logger=Logger.getLogger(getClass());
	/**对应标签的属性*/
	private Integer totalCount=0;
	private Integer currentPage=1;
	private Integer pageSize=1;
	
	private String name="";//分页标签的属性名称
	private String includes="";	//get传递参数的，以逗号隔开
	private String pagerForm="";//post传递参数时使用的查询表单名称
	private Integer seaJS=0;    //是否使用seaJs包装.缺省不使用.直接使用Jquery
	@Override
	public Component getBean(ValueStack arg0, HttpServletRequest arg1,HttpServletResponse arg2) {
		return new Pages(arg0);
	}
	/**直接把得到的值赋值给组件对象.此处的组件对象是pages对象*/
	protected void populateParams() {
		super.populateParams();
		logger.debug("populateParams()");		
		PageUtil pageUtil=new PageUtil();
		pageUtil.setTotalCount(getTotalCount());
		pageUtil.setPageSize(getPageSize());
		pageUtil.setCurrentPage(getCurrentPage());
		//把标签属性的值传递到组件当中
		Pages pages = (Pages) component;
		pages.setIncludes(getIncludes());
		pages.setName(getName());
		pages.setPagerForm(getPagerForm());
		pages.setPageUtil(pageUtil);
		pages.setSeaJS(getSeaJS());
		logger.debug("标签传递参数:"+toString());
	}	
	/**总记录数*/
	public Integer getTotalCount() {
		return totalCount==null||totalCount<0?0:totalCount;
	}
	/**总记录数*/
	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount==null||totalCount<0?0:totalCount;
	}
	/**当前要显示的页号*/
	public Integer getCurrentPage() {
		return currentPage==null||currentPage<1?1:currentPage;
	}
	/**当前要显示的页号*/
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage==null||currentPage<1?1:currentPage;;
	}
	/**每页显示记录条数*/
	public Integer getPageSize() {
		return pageSize==null||pageSize<1?1:pageSize;
	}
	/**每页显示记录条数*/
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize==null||pageSize<1?1:pageSize;;
	}
	/**需要传递的属性*/
	public void setIncludes(String includes) {
		this.includes = includes==null?"":includes;
	}
	/**需要传递的属性*/
	public  String getIncludes() {
		return includes==null?"":includes;
	}
	/**分页对象的名称.作为分页参数的前缀比如，
	 * 当name=page时应传递:page.currentPage*/
	public String getName() {
		return name==null?"":name;
	}
	/**分页对象的名称.作为分页参数的前缀比如，
	 * 当name=page时应传递:page.currentPage*/
	public void setName(String name) {
		this.name = name==null?"":name;
	}
	@Override
	public String toString() {
		return "PageTag [totalCount=" + totalCount
				+ ", currentPage=" + currentPage + ", pageSize=" + pageSize
				+ ", name=" + name + ", includes="
				+ includes + "]";
	}
	/**页面传递过来的。用户生成的提交的表单的名字
    * 如果为空则通过a标签提交*/
	public String getPagerForm() {
		return pagerForm==null?"":pagerForm;
	}
	/**页面传递过来的。用户生成的提交的表单的名字
	 * * 如果为空则通过a标签提交*/
	public void setPagerForm(String pagerForm) {
		this.pagerForm = pagerForm==null?"":pagerForm;
	}
	/**Jquery是否使用seajs包装0，不使用，1使用*/
	public Integer getSeaJS() {
		return seaJS==null?0:seaJS;
	}
	/**Jquery是否使用seajs包装0，不使用，1使用*/
	public void setSeaJS(Integer seaJS) {
		this.seaJS = seaJS;
	}
}